package com.huangfu.learning.javabase.web.controller;

import com.huangfu.learning.javabase.web.entity.User;
import com.huangfu.learning.javabase.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/8/18
 * Time: 14:23
 * Version: 1.0
 * Description:
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String test() {
        return "list";
    }

    @RequestMapping("/index")
    public String index() {
        return "login";
    }

    @RequestMapping("/getUserById")
    public User getUserById(String id) {
        return userService.getUserById(id);
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        User loginUser = userService.login(user);
        if (loginUser != null) {
            request.getSession().setAttribute("userName", loginUser.getUsername());
            return "success";
        }
        request.getSession().setAttribute("message", "用户名或密码有误！！！");
        System.out.println(loginUser.getUsername());
        return "login";
    }
}
